home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
090
/
byte0887.arc
/
LANE.ARC
/
TOKENS80.ARI
< prev
Wrap
Text File
|
1987-05-05
|
3KB
|
80 lines
% Subject: TOKENS80.ARI - from A. Lane: "Simulating an 8085 with Prolog"
% This version of get_token_list(_) assumes all input numbers are in
% hexadecimal and delivers decimal numbers in the output. i.e.,
% entering 'show 0f' results in [show,15].
get_token_list(Result) :- % read a sentence from the terminal
read_line(0,String), % read a line of input from the console
list_text([Char|Tail],String),
tokenize([Char|Tail], [], Result).
tokenize([H|T],List,L) :- % if head of list starts with letter
letter(H, Letter),!,
restword(T,[Letter],Word,Rem), % get the rest of a word
append(List,[Word],Nlist),
tokenize(Rem,Nlist,L). % recurse, tokenize rest of list
tokenize([H|T], List, L) :- % if head of list starts with digit (0-9)
digit(H),!,
rest_num(T,[H],Num, Rem), % get a number (Num will be decimal)
append(List,[Num],Nlist),
tokenize(Rem,Nlist,L). % recurse, tokenize rest of list
tokenize([_|T], List, L) :- % if head of list is not letter or digit,
!, tokenize(T,List,L). % ignore it.
tokenize([],List,List). % stop recursion.
restword( [H|T], List, Word, X ) :-
letter( H, Letter ),!,
append( List, [Letter], Nlist ),
restword( T, Nlist, Word, X ).
restword( [32|T], List, Word, T ) :-
name( Word, List ),!.
restword( [_|T], List, Word, X ) :-
!, restword( T, List, Word, X ).
restword( [], List, Word, [] ) :-
!, name( Word, List ).
rest_num( [H|T], List, Num, X ) :-
hexdigit( H,_ ) , !, % rest of number may have 0-9, a-f
append( List, [H], Nlist ),
rest_num( T, Nlist, Num, X ).
rest_num( [32|T], List, Num, T ) :- % space finishes number, go convert.
cname( Num, List, 0 ),!.
rest_num( [],List,Num,[]) :- % nothing left, go convert.
!, cname(Num, List, 0).
cname( F, [X|[]], N) :- % finished.
!, hexdigit(X,Y), F is N + Y.
cname( Number, [H|T] ,In ) :- % convert the input hex number to
hexdigit(H,H1), % a decimal. User need never know!
H2 is (In + H1) * 16,
cname(Number, T, H2). % recurse with shorter list in T
letter(C, C) :-
C >= 97, C =< 122, !. % 97 is "a", 122 is "z"
letter(C, D) :-
C >= 65, C =< 90, !, % 65 is "A", 90 is "Z"
D is C + 32. % 32 is "a"-"A"
hexdigit(D,E) :-
digit(D), E is D - 48.
hexdigit(D,F) :-
letter(D,E), E >= 97, E =< 102,
F is E - 87.
digit(C) :-
C >= 48, C =< 57. % 48 is "0", 57 is "9".
%
% end: TOKENS80.ARI